﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Core;
using System.Collections.Generic;
using Jungle.Code.Utils;
using Jungle.Code.DAL;

    public partial class login : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SessionKeeper thisSession = new SessionKeeper();
            thisSession.SessionId = Session.SessionID.ToString();
            thisSession.GetData();

            if (thisSession.AdminLogin != "administrator")
            {
                loginBody.Visible = true;
                loginFon.Visible = true;

                string jScript = "<script language=javascript>";
                jScript += "document.getElementById('cms').style.overflow = \"hidden\";";
                jScript += "var scrollEnabled = 'false';";
                jScript += "</script>";

                Page.RegisterStartupScript("crollHidder", jScript);
            }
        }

        protected void Page_Init(object sender, EventArgs e)
        {
            this.Load += new EventHandler(this.Page_Load);
        }

        public bool Login(string _login, string _pass)
        {
            bool valid = false;
            using (SqlConnection lCon = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand com_login = new SqlCommand("admin_Login", lCon);
                SqlParameter param_login = com_login.Parameters.Add("@param_login", SqlDbType.NChar, 50);
                SqlParameter param_pass = com_login.Parameters.Add("@param_pass", SqlDbType.NChar, 20);
                SqlParameter param_valid = com_login.Parameters.Add("@param_valid", SqlDbType.Bit);
                
                com_login.CommandType = CommandType.StoredProcedure;

                param_login.Value = _login;
                param_pass.Value = _pass;
                param_valid.Direction = ParameterDirection.Output;
                param_valid.Value = null;

                com_login.Connection.Open();
                    com_login.ExecuteNonQuery();
                    valid = (bool)param_valid.Value;
                com_login.Connection.Close();
            }
            return valid;
        }

        public void Enter(object sender, EventArgs e)
        {
            if (Login(user_name.Text, password.Text))
            {
                SessionKeeper thisSession = new SessionKeeper();
                thisSession.SessionId = Session.SessionID.ToString().Trim();
                thisSession.UserId = -1;
                thisSession.UserLogin = "none";
                thisSession.AdminLogin = "administrator";
                thisSession.Create();

                Response.Redirect("~/Admin/Default.aspx");
            }
        }
    }
